package com.zk.yygh.hosp.exception;

import com.zk.yygh.hosp.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常处理
 *
 * @author: 张凯
 * @date: 2022/5/12 - 21:27
 */
@Api("全局异常处理")
@ControllerAdvice
public class GlobalExceptionHandler {

    @ApiOperation("全局异常处理")
    @ExceptionHandler(Exception.class)  //接收指定异常触发调用
    @ResponseBody    //返回以json格式数据
    public Result error(Exception e) {
        e.printStackTrace();    //打印异常信息
        return Result.fail();
    }

    @ApiOperation("自定义异常处理")
    @ExceptionHandler(HospitalException.class)
    @ResponseBody
    public Result error(HospitalException hospitalException) {
        //构造Result，无data数据构造(将自定义异常信息的code以及msg构造)
        return Result.build(hospitalException.getCode(), hospitalException.getMessage());
    }
}
